import 'package:flutter/material.dart';
import 'package:module_home/model/ISSDataEntity.dart';
import 'package:module_home/model/ScoreEntity.dart';
import 'package:module_home/model/TransferDataEntity.dart';
import 'package:module_home/widget/IssTop.dart';
import 'package:module_home/widget/MenuDataItem.dart';
import 'package:lib_ui/widget/appbar_widget.dart';
import 'package:module_home/widget/ScoreItem.dart';
import 'package:lib_navigator/custom_route.dart';

import 'Iss_datal_page.dart';

///某部位页面
class MenuDataPage extends StatefulWidget {
  final ISSDataEntity issDataEntity;
  final List<ScoreEntity> list;

  const MenuDataPage({Key key, this.list, this.issDataEntity})
      : super(key: key);

  @override
  _MenuDataPageState createState() => _MenuDataPageState();
}

class _MenuDataPageState extends State<MenuDataPage> {
  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () => _backToData(context),
        child: Scaffold(
          backgroundColor: Colors.white,
          appBar:
              appBar(widget.issDataEntity.name, showBack: false, toBack: () {
            _backToData(context);
          }),
          body: Column(
            children: [
              ISSTop(
                title: widget.issDataEntity.name,
                img: widget.issDataEntity.imgUrlCheck,
              ),
              Container(
                  width: double.infinity,
                  height: 48,
                  color: Colors.grey.shade100,
                  child: Row(
                    children: _buildItem(widget.list),
                  )),
              Expanded(
                  child: ListView.builder(
                      itemCount: widget.issDataEntity.childList.length,
                      itemBuilder: (context, index) {
                        return GestureDetector(
                          child: MenuDataItem(
                              childList: widget.issDataEntity.childList[index]),
                          onTap: () {
                            _toTransferForResult(
                                context, widget.issDataEntity.childList[index]);
                          },
                        );
                      })),
            ],
          ),
        ));
  }

  List<Widget> _buildItem(List<ScoreEntity> items) {
    return items
        .map((item) => Expanded(child: ScoreItem(scoreModel: item)))
        .toList();
  }

  ///跳转的时候我们需要使用异步等待回调结果 dataFromOtherPage 就是返回的结果
  _toTransferForResult(BuildContext context, ChildList childList) async {
    final dataFromOtherPage = await Navigator.push(
      context,
      CustomRoute( ISSDetailPage(childList: childList)),
    ) as TransferDataEntity;

    setState(() {
      List list = [];
      int aisValue = 0;
      widget.issDataEntity.childList.forEach((element) {
        if (element.issValue > 0) list.add(element.issValue);
        if (element.aisValue > 0) aisValue = aisValue + element.aisValue;
        // print("某部位页面" + element.issValue.toString() +",aisValue"+element.aisValue.toString());
      });
      widget.issDataEntity.aisValue = aisValue;
      if (list.length == 0) {
        widget.issDataEntity.issValue = 0;
        // print("某部位页面" +
        //     widget.issDataEntity.issValue.toString() +
        //     ",aisValue" +
        //     widget.issDataEntity.aisValue.toString());
      } else {
        list.sort();
        widget.issDataEntity.issValue = list.last;
        // print("某部位页面" +
        //     widget.issDataEntity.issValue.toString() +
        //     ",aisValue" +
        //     widget.issDataEntity.aisValue.toString());
      }
    });
  }

//返回并携带数据
  _backToData(BuildContext context) {
    var transferData = TransferDataEntity(
        widget.issDataEntity.aisValue, widget.issDataEntity.issValue);
    Navigator.pop(context, transferData);
  }
}
